﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class User_Fee_Add : System.Web.UI.Page
{
    protected int FeeKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["MenuType"]) ? "" : Request["MenuType"].ToString(); }
    }

    protected int UserType,UserKey;

    protected void Page_Load(object sender, EventArgs e)
    {
        FeeKey = string.IsNullOrEmpty(Request["FeeKey"]) ? 0 : Convert.ToInt32(Request["FeeKey"]);

        UserType = Convert.ToInt32(Session["UserType"]);
        UserKey = Convert.ToInt32(Session["UserKey"]);

        if (!this.IsPostBack)
        {
            txt_FeeTime.Attributes.Add("onfocus", "selectDate(this)");


            if (UserType == 4)    //管理员可以选择员工
            {
                IList<Sys_Account> UserList = new Account().GetNotAccount(1);
                foreach (Sys_Account account in UserList)
                    txt_MUserKey.Items.Add(new ListItem("◆ " + account.RealName, account.UserKey.ToString()));
            }

            if (UserType == 3)    //工程师只能选择自己
            {
                txt_MUserKey.Items.RemoveAt(0);
                txt_MUserKey.Items.Add(new ListItem("◆ " + new Account().GetAccountName(UserKey), UserKey.ToString()));
            }

            if (FeeKey != 0)
            {   
                Dat_Fee fee = new Fee().SelectFee(FeeKey);
                txt_MUserKey.SelectedValue = fee.MUserKey.ToString();
                txt_FeeTime.Text = fee.FeeTime.ToString("yyyy-MM-dd");
                txt_TransFee.Text = fee.TransFee.ToString();
                txt_FoodFee.Text = fee.FoodFee.ToString();
                txt_OtherFee.Text = fee.OtherFee.ToString();
                txt_Remark.Text = fee.Remark;

                btnSubmit.Text = "修 改";
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        decimal? de = null;

        if (FeeKey == 0)
        {
            Dat_Fee fee = new Dat_Fee
            {   
                DanNum = txt_DanNum.Text,
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                FeeTime = Convert.ToDateTime(txt_FeeTime.Text.Trim()),
                TransFee = txt_TransFee.Text.Trim()==""?de:Convert.ToDecimal(txt_TransFee.Text.Trim()),
                FoodFee = txt_FoodFee.Text.Trim()==""?de:Convert.ToDecimal(txt_FoodFee.Text.Trim()),
                OtherFee = txt_OtherFee.Text.Trim()==""?de:Convert.ToDecimal(txt_OtherFee.Text.Trim()),
                CreateTime = DateTime.Now,
                Remark = txt_Remark.Text.Trim(),
                UserKey = Convert.ToInt16(Session["UserKey"])
            };

            if (new Fee().AddFee(fee))
            {
                WebUtility.JsAlertToGo("添加费用成功", "Fee_Add.aspx?menutype=231");
            }
        }
        else
        {
            Dat_Fee fee = new Dat_Fee
            {   
                FeeKey = FeeKey,
                DanNum = txt_DanNum.Text,
                MUserKey = Convert.ToInt32(txt_MUserKey.SelectedValue),
                FeeTime = Convert.ToDateTime(txt_FeeTime.Text.Trim()),
                TransFee = txt_TransFee.Text.Trim() == "" ? de : Convert.ToDecimal(txt_TransFee.Text.Trim()),
                FoodFee = txt_FoodFee.Text.Trim() == "" ? de : Convert.ToDecimal(txt_FoodFee.Text.Trim()),
                OtherFee = txt_OtherFee.Text.Trim() == "" ? de : Convert.ToDecimal(txt_OtherFee.Text.Trim()),
                //CreateTime = DateTime.Now,
                Remark = txt_Remark.Text.Trim()
                //UserKey = 6
            };

            if (new Fee().EditFee(fee))
            {
                WebUtility.JsAlertToGo("修改费用成功", "Fee.aspx?menutype=232");
            }
        }
    }
}
